<?php

namespace app\admin\model\pifa;

use think\Model;
use traits\model\SoftDelete;
use app\admin\model\pifa\ActivitySku;
use app\admin\model\litestore\Litestoregoodsspec as SpecModel;
use app\admin\model\litestore\Litestoregoods;
use think\Db;

class Activity extends Model
{


    

    // 表名
    protected $name = 'pifa_activity';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text'
    ];
        

     protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

     public function getGoodsListAttr($value, $data)
    {
        if ($data['goods_ids']) {
            
            $goods = Litestoregoods::field('goods_id,goods_name,image,desc')->whereIn('goods_id', $data['goods_ids'])->select();
            $goods = collection($goods)->toArray();             // 全部转数组
            
            $goodsIds = array_column($goods, 'goods_id');


            // $activitySkuPrices = ActivitySku::where('activity_id', $data['id'])->whereIn('goods_id', $goodsIds)->order('id', 'asc')->select();
            
            $map1['activity_id'] = $data['id'];
            $map1['goods_id'] = ['in',$goodsIds];
            $activitySkuPrices = ActivitySku::where($map1)->order('id', 'asc')->select();

            $activitySkuPrices = collection($activitySkuPrices)->toArray();
            
            // 后台编辑活动时，防止不编辑规格无法提交问题
            foreach ($goods as &$gd) {
                // 处理 $gd['activity_sku_prices']
                $gd['activity_sku_prices'] = [];
                foreach ($activitySkuPrices as $skuPrice) {


                    if ($skuPrice['goods_id'] == $gd['goods_id']) {

                        $map2['goods_spec_id'] = $skuPrice['goods_sku_price_id'];
                        $info = Db::table('fa_litestore_goods_spec')->where($map2)->cache(true)->find();
                        if (!empty($info)) {
                            $skuPrice['org_price'] = $info['goods_price'];
                        }
                        $gd['activity_sku_prices'][] = $skuPrice;
                    }
                }

                // 处理活动规格,数据
                foreach ($gd['activity_sku_prices'] as $key => $skuPrice) {
                    // $skuPrice = ActivityFacade::showSkuPrice($data['type'], $skuPrice);
                    // $gd['activity_sku_prices'][$key] = $skuPrice;
                }
            }
        }

        return $goods ?? [];
    }

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


     public function shangjia()
    {
        return $this->belongsTo('app\admin\model\pifa\Shangjia', 'shangjia_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
